package com.pucong.gitee.database.backup.business.database.controller;

import com.pucong.gitee.database.backup.business.database.service.OperationDataTableService;
import com.pucong.gitee.database.backup.common.entity.database.dto.*;
import com.pucong.gitee.database.backup.common.entity.database.vo.DatabaseTableFiledVo;
import com.pucong.gitee.database.backup.common.entity.database.vo.DatabaseTableIndexVo;
import com.pucong.gitee.database.backup.common.entity.database.vo.DatabaseTableVo;
import com.pucong.gitee.database.backup.common.entity.database.vo.RunSqlResultPage;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.utils.Res;
import com.pucong.gitee.database.backup.utils.UserRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pucong
 * @url https://gitee.com/pucong
 * @create 2022-03-02
 */
@RestController
@RequestMapping("/operation/dataTable")
@Api(value = "数据表操作-API ")
public class OperationDataTableController {

    @Autowired
    private OperationDataTableService operationDataTableService;

    @GetMapping("/getTableList")
    @ApiOperation(value = "获取表信息")
    public Res<List<DatabaseTableVo>> getTableLit(DatabaseTableDto dto) {
        List<DatabaseTableVo> databaseTableList = operationDataTableService.getTableList(dto);
        return Res.ok(databaseTableList);
    }

    @GetMapping("/getTableData")
    @ApiOperation(value = "获取表数据")
    public Res<RunSqlResultPage> getTableData(DatabaseTableDataDto dto) {
        UserJwt loginUser = UserRequest.getLoginUser();
        RunSqlResultPage runSqlResultPage = operationDataTableService.getTableData(loginUser.getUuid(), dto);
        return Res.ok(runSqlResultPage);
    }

    @PostMapping("/getDataDmlSql")
    @ApiOperation(value = "获取表数据")
    public Res<String> getDataDmlSql(@Validated @RequestBody DatabaseTableDataDmlDto dto) {
        List<String> dataDmlSql = operationDataTableService.getDataDmlSql(dto);
        return Res.ok(dataDmlSql);
    }

    /**** 字段api start ***/
    @GetMapping("/getTableFiledList")
    @ApiOperation(value = "获取表列信息")
    public Res<List<DatabaseTableFiledVo>> getTableFiledList(DatabaseTableDataDto dto) {
        List<DatabaseTableFiledVo> voList = operationDataTableService.getTableFiledList(dto);
        return Res.ok(voList);
    }

    @PostMapping("/alterPk")
    @ApiOperation(value = "上移")
    public Res<Boolean> alterPk(@Validated @RequestBody DatabaseTableFiledPkDto dto) {
        Boolean aBoolean = operationDataTableService.alterPk(dto);
        return Res.ok(aBoolean);
    }

    @PostMapping("/toUp")
    @ApiOperation(value = "上移")
    public Res<Boolean> toUp(@Validated @RequestBody DatabaseTableFiledDto dto) {
        Boolean aBoolean = operationDataTableService.toUp(dto);
        return Res.ok(aBoolean);
    }

    @PostMapping("/toDown")
    @ApiOperation(value = "下移")
    public Res<Boolean> toDown(@Validated @RequestBody DatabaseTableFiledDto dto) {
        Boolean aBoolean = operationDataTableService.toDown(dto);
        return Res.ok(aBoolean);
    }

    @PostMapping("/addFiled")
    @ApiOperation(value = "新增字段")
    public Res<Boolean> addFiled(@Validated @RequestBody DatabaseTableFiledAddDto dto) {
        Boolean aBoolean = operationDataTableService.addFiled(dto);
        return Res.ok(aBoolean);
    }

    @PostMapping("/updateFiled")
    @ApiOperation(value = "修改字段")
    public Res<Boolean> updateFiled(@Validated @RequestBody DatabaseTableFiledUpdateDto dto) {
        Boolean aBoolean = operationDataTableService.updateFiled(dto);
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteFiled")
    @ApiOperation(value = "删除字段")
    public Res<Boolean> deleteFiled(@Validated @RequestBody DatabaseTableFiledDto dto) {
        Boolean aBoolean = operationDataTableService.deleteFiled(dto);
        return Res.ok(aBoolean);
    }

    @GetMapping("/getTableFiledTypeList")
    @ApiOperation(value = "获取字段类型list")
    public Res<String[]> getTableFiledTypeList(String dataSourceId) {
        String[] voList = operationDataTableService.getTableFiledTypeList(dataSourceId);
        return Res.ok(voList);
    }
    /**** 字段api end ***/

    /**** 索引api start ***/
    @GetMapping("/getTableIndexList")
    @ApiOperation(value = "获取表索引信息")
    public Res<List<DatabaseTableIndexVo>> getTableIndexList(DatabaseTableIndexSelectDto dto) {
        List<DatabaseTableIndexVo> voList = operationDataTableService.getTableIndexList(dto);
        return Res.ok(voList);
    }

    @GetMapping("/getTableIndexTypeList")
    @ApiOperation(value = "获取表索引信息")
    public Res<String[]> getTableIndexTypeList(DatabaseTableIndexSelectDto dto) {
        String[] voList = operationDataTableService.getTableIndexTypeList(dto);
        return Res.ok(voList);
    }

    @PostMapping("/addIndex")
    @ApiOperation(value = "新增索引")
    public Res<Boolean> addIndex(@Validated @RequestBody DatabaseTableIndexAddDto dto) {
        Boolean aBoolean = operationDataTableService.addIndex(dto);
        return Res.ok(aBoolean);
    }

    @PostMapping("/updateIndex")
    @ApiOperation(value = "修改索引")
    public Res<Boolean> updateIndex(@Validated @RequestBody DatabaseTableIndexUpdateDto dto) {
        Boolean aBoolean = operationDataTableService.updateIndex(dto);
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteIndex")
    @ApiOperation(value = "删除索引")
    public Res<Boolean> deleteIndex(@Validated @RequestBody DatabaseTableIndexDelDto dto) {
        Boolean aBoolean = operationDataTableService.deleteIndex(dto);
        return Res.ok(aBoolean);
    }

    /**** 索引api end ***/


}
